﻿using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;

using BLL.Services.Books.Listing;

namespace BLL.Services.Books
{
    public class BooksService : IBookService
    {
        private readonly BooksCollection _booksCollection;

        /// <summary>
        /// Initializes a new instance of the <see cref="BooksService" /> class.
        /// </summary>
        /// <param name="collection">The collection.</param>
        public BooksService(BooksCollection collection)
        {
            _booksCollection = collection;
        }

        /// <summary>
        /// Gets the get all books.
        /// </summary>
        /// <value>
        /// The get all books.
        /// </value>
        public IEnumerable<Book> GetAllBooks
        {
            get
            {
                return _booksCollection.SelectAll();
            }
        }

        /// <summary>
        /// Gets the book by id.
        /// </summary>
        /// <param name="bookId">The book id.</param>
        public Book GetBookById(int bookId)
        {
            return _booksCollection.SelectById(bookId);
        }

        /// <summary>
        /// Gets the books by author.
        /// </summary>
        /// <param name="authorId">The author id.</param>
        public IEnumerable<Book> GetBooksByAuthor(int authorId)
        {
            //return _booksCollection.SelectByAuthor(authorId);
            return _booksCollection.SelectAll().Take(10);
        }

        public IEnumerable<BookShortInfoModel> GetSomeBooksByCategory(
            int categoryId, int userId, string sortOption, int sortDirection, int page = 1, int size = 12)
        {
            return _booksCollection.GetSomeBooksByCategory(
                categoryId, userId, page, size, sortOption, sortDirection);
        }

        /// <summary>
        /// Gets the related book.
        /// </summary>
        public IEnumerable<Book> GetRelatedBook()
        {
            return _booksCollection.SelectAll().Take(10);
        }

        /// <summary>
        /// Creates the specified book.
        /// </summary>
        /// <param name="book">The book.</param>
        public void Create(Book book)
        {
            try
            {
                _booksCollection.Create(book);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        /// <summary>
        /// Saves the specified book.
        /// </summary>
        /// <param name="book">The book.</param>
        public void Save(Book book)
        {
            try
            {
                _booksCollection.Save(book);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public bool ValidateBook(int bookId, string name, int[] authors)
        {
            try
            {
                return _booksCollection.ValidateBook(bookId, name, authors);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        /// <summary>
        /// Deletes the specified book id.
        /// </summary>
        /// <param name="bookId">The book id.</param>
        public void Delete(int bookId)
        {
            try
            {
                _booksCollection.Delete(bookId);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        /// <summary>
        /// Gets the new books.
        /// </summary>
        public IEnumerable<Book> GetNewBooks()
        {
            return _booksCollection.SelectAll().Take(4);
        }

        /// <summary>
        /// Gets the best books.
        /// </summary>
        /// <param name="userId">The user id.</param>
        public IEnumerable<BookWithRating> GetBestBooks(int? userId)
        {
            return _booksCollection.GetBestBooks(userId);
        }

        /// <summary>
        /// Gets the listing book.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="size">The size.</param>
        /// <param name="column">The column.</param>
        /// <param name="direction">The direction.</param>
        /// <param name="filter">The filter.</param>
        public BooksListing GetListingBook(
            int page, int size, string column, int direction, BookFilter filter)
        {
            try
            {
                return _booksCollection.GetListingBook(page, size, column, direction, filter);
            }
            catch (SqlException ex)
            {
                throw new ServiceException(ex.Message);
            }
        }


        public IEnumerable<BookShortInfoModel> GetProfileRatedBooks(
            string email, string column, int direction, int currentUserid, int page = 1, int size = 12)
        {
            try
            {
                return _booksCollection.GetProfileRatedBooks(email, page, size, column, direction, currentUserid);
            }
            catch (SqlException ex)
            {
                throw new ServiceException(ex.Message);
            }
        }
    }
}
